package com.tongcao.cn.contoller;

import com.robert.vesta.service.bean.Id;
import com.tongcao.cn.dfire.result.ResultMap;
import com.tongcao.cn.dfire.utils.ApiResultUtil;
import com.tongcao.cn.service.UidService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @Author:tongcao
 * @Date:2019/1/10 16:09
 * Description:唯一id API
 */
@Controller
public class UidController {

    @Resource
    private UidService uidService;

    /**
     * 返回生成的全局唯一 ID流水号
     *
     * @return
     */
    @RequestMapping(value = "/genid",method = RequestMethod.GET)
    @ResponseBody
    public ResultMap genId() {
        long genId = uidService.genId();
        return ApiResultUtil.successResult(genId);
    }

    /**
     * 反解出流水号所代表的意义
     *
     * @param id
     * @return
     */
    @RequestMapping("/expid")
    @ResponseBody
    public ResultMap explainId(@RequestParam(value = "id", defaultValue = "0") long id) {
        Id explainId = uidService.explainId(id);
        return ApiResultUtil.successResult(explainId);

    }

    @RequestMapping("/makeid")
    @ResponseBody
    public ResultMap makeId(
            @RequestParam(value = "version", defaultValue = "-1") long version,
            @RequestParam(value = "type", defaultValue = "-1") long type,
            @RequestParam(value = "genMethod", defaultValue = "-1") long genMethod,
            @RequestParam(value = "machine", defaultValue = "-1") long machine,
            @RequestParam(value = "time", defaultValue = "-1") long time,
            @RequestParam(value = "seq", defaultValue = "-1") long seq) {
        long makeId = uidService.makeId(version, type, genMethod, machine, time, seq);
        return ApiResultUtil.successResult(makeId);
    }
}
